<?php
	require_once('conf/top.php');
	$template = $tpl->loadTemplate('profil.html');

	connectDb();
	session_start();

	$erreurs = null;
	$infos = null;

	$theme = theme(); // Gérer dans lib.
	$avaibilities = null;
	$profil = null;
	$checkInsert = 0;
	$reccurence_get = null;
	$categorieUser = null;

	if(isset($_SESSION['id_user']) && !empty($_SESSION['id_user'])){
		if(!isset($_GET['id'])) {
			$_GET['id'] = $_SESSION['id_user'];
		}
		
		if(isset($_GET['id']) && !empty($_GET['id'])){	
			$id_user = $_GET['id'];
			$avaibilities = getAllAvailabilityFromUser($id_user);
			$profil = getUserById($id_user);
			$categoriesUser = getSkillFromUser($id_user);
			$listCategoriesUser = null;
			foreach ($categoriesUser as $value){
				$listCategoriesUser .= "$value[name] ";
			}
			if(isset($_GET['insertCat'])){
				$infos[] = "Vos catégories ont bien été prises en compte.";
			}
			if(isset($_GET['suppCat'])){
				$infos[] = "Vos catégories ont bien été supprimées.";
			}
			if(isset($_GET['updateInfos'])){
				$infos[] = "Votre mot de passe à bien été mis à jour.";
			}
		}

		if(isset($_POST['supprimerAvailability'])){
			if(isset($_POST['id_availability']) && !empty($_POST['id_availability'])){
				$_POST['id_availability'] = htmlspecialchars(stripslashes($_POST['id_availability']));
			    $_POST['id_availability'] = str_ireplace("script", "blocked", $_POST['id_availability']);
				$id_availability = $_POST['id_availability'];
				$check = updateAvailabilityActive($id_availability);
				$avaibilities = getAllAvailabilityFromUser($id_user);
				if($check){
					$infos[] = "Votre disponibilité a bien été supprimée.";
				}
			}
		}
		
		if(isset($_POST['ajouterAvailability'])){
			if (isset($_POST['date_begin']) && isset($_POST['duration']) && isset($_POST['price']) && isset($_POST['reccurence']) && isset($_POST['time'])) {
				if(isset($_POST['date_begin']) && !empty($_POST['date_begin'])){
					$_POST['date_begin'] = htmlspecialchars(stripslashes($_POST['date_begin']));
				    $_POST['date_begin'] = str_ireplace("script", "blocked", $_POST['date_begin']);
					$date_begin = $_POST['date_begin'];
				}
				if(isset($_POST['duration']) && !empty($_POST['duration'])){
					$_POST['duration'] = htmlspecialchars(stripslashes($_POST['duration']));
				    $_POST['duration'] = str_ireplace("script", "blocked", $_POST['duration']);
					$duration = $_POST['duration'];
				}
				if(isset($_POST['price']) && !empty($_POST['price'])){
					$_POST['price'] = htmlspecialchars(stripslashes($_POST['price']));
				    $_POST['price'] = str_ireplace("script", "blocked", $_POST['price']);
					$price = $_POST['price'];
				}
				if(isset($_POST['reccurence']) && !empty($_POST['reccurence'])){
					$_POST['reccurence'] = htmlspecialchars(stripslashes($_POST['reccurence']));
				    $_POST['reccurence'] = str_ireplace("script", "blocked", $_POST['reccurence']);
					$reccurence = $_POST['reccurence'];
				}
				if(isset($_POST['time']) && !empty($_POST['time'])){
					$_POST['time'] = htmlspecialchars(stripslashes($_POST['time']));
				    $_POST['time'] = str_ireplace("script", "blocked", $_POST['time']);
					$time = $_POST['time'];
				}
				$date = $date_begin.' '.$time;
				if(!empty($categoriesUser)){
					$checkInsert = setAvailabilityToUser($_SESSION['id_user'], $date, $duration, $reccurence, $price);
				}
				else{
					$infos[] = "Vous devez avoir au moins une catégorie pour créer une disponibilité.";
				}
				
				$avaibilities = getAllAvailabilityFromUser($id_user);
				if($checkInsert != 0){
					$infos[] = "Votre disponibilité a bien été rajoutée.";
				}
			}
		}

		/* On envoie le necessaire à notre template */
		echo $template->render(array('MEDIA_PATH' => MEDIA_PATH, 'id_user' => $id_user, 'erreurs' => $erreurs, 'infos' => $infos, 'theme' => $theme, 'avaibilities' => $avaibilities, 'session' => $_SESSION, 'profil' => $profil, 'litCategoriesUser' => $listCategoriesUser));
	}
	else{
		header('location:index.php');
	}